﻿using Mes.Pendant.CustomerManagements;
using Mes.Pendant.CustomerManagements.DTO;
using Mes.Pendant.CustomerManagementTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.MaterialManagementServicess
{
    public class CustomerManagementAppService : ApplicationService, ICustomerManagementAppService
    {
        private readonly IRepository<CustomerManagement, int> _repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public CustomerManagementAppService(IRepository<CustomerManagement, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="customerId">主键Id</param>
        /// <returns></returns>
        public int DeleteCustomerManagementInfo(int customerId)
        {
            CustomerManagement? entity = _repository.GetListAsync().Result.FirstOrDefault(c => c.Id == customerId);
            _repository.DeleteAsync(entity!);
            return 1;
        }

        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="customerCode">客户编号</param>
        /// <param name="customerName">客户名称</param>
        /// <param name="isEffective">信息</param>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        public (List<GetCustomerManagementDTO> list, int total) GetCustomerManagementInfo(string? customerCode, string? customerName, int page, int limit)
        {
            var list = PredicateBuilder.New<CustomerManagement>();

            if (!string.IsNullOrEmpty(customerCode))
            {
                list = list.And(x => x.CustomerCode.Contains(customerCode));
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                list = list.And(x => x.CustomerName.Contains(customerName));
            }
            var result = _repository.GetListAsync().Result;

            var total = result.Count;
            result = result.Skip((page - 1) * limit).Take(limit).ToList();
            return (ObjectMapper.Map<List<CustomerManagement>, List<GetCustomerManagementDTO>>(result), total);
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="customerDto">实体</param>
        /// <returns></returns>
        public int PostCustomerManagementInfo(CreateCustomerManagementDTO customerDto)
        {
            var entity = ObjectMapper.Map<CreateCustomerManagementDTO, CustomerManagement>(customerDto);
            _repository.InsertAsync(entity);
            return 1;
        }

        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public GetCustomerManagementDTO GetCustomerById(int id)
        {
            var entity = _repository.GetAsync(id).Result;
            var customer = ObjectMapper.Map<CustomerManagement, GetCustomerManagementDTO>(entity);
            return customer;
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="customerDto">实体</param>
        /// <returns></returns>
        public int PutCustomerManagementInfo(UpdateCustomerManagementDTO customerDto)
        {
            var entity = ObjectMapper.Map<UpdateCustomerManagementDTO, CustomerManagement>(customerDto);
            _repository.UpdateAsync(entity);
            return 1;
        }
    }
}
